package saucaca.fragment;

import java.util.ArrayList;

import saucaca.Adapter.ExpandableSongAdapter2;
import saucaca.CustomView.CircularImageView;
import saucaca.Object.AlbumObject;
import saucaca.Object.SongObject;
import saucaca.Util.AppController;
import saucaca.Util.TagName;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lemon.kenhnhacvn.BaseActivity;
import com.lemon.kenhnhacvn.BaseEngine;
import com.lemon.kenhnhacvn.R;

public class AlbumDetaisContent extends Fragment {
	AlbumObject albumObject;
	Context context;
	String albumID = "";
	TextView txtTitle, txtArtist;
	CircularImageView albumPic;
	private ExpandableListView expandView;
	ArrayList<SongObject> listSongs;
	ExpandableSongAdapter2 adapter;
	private View mView_EmptyLoading = null;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	TextView txtStatus;
	ImageLoader imageLoader;
	RelativeLayout shareAlbum;
	
	public AlbumDetaisContent(Context context, AlbumObject albumObject, ArrayList<SongObject> listSongs) {
		this.albumObject = albumObject;
		this.context = context;
		this.listSongs = listSongs;
	}

	public void update(AlbumObject albumObject, ArrayList<SongObject> listSongs) {
		this.albumObject = albumObject;
		this.listSongs = listSongs;
		showAlbumDetail(albumObject);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.album_details_main, container, false);
		try {
			imageLoader=((BaseActivity)getActivity()).imageLoader;
		} catch (Exception ex) {
			imageLoader=AppController.getInstance().getImageLoader();
		}
		findviewbyid(view);
		showAlbumDetail(albumObject);
		return view;
	}

	public void findviewbyid(View view) {
		albumPic = (CircularImageView) view.findViewById(R.id.album_details_picture);
		txtArtist = (TextView) view.findViewById(R.id.album_details_txtArtist);
		txtTitle = (TextView) view.findViewById(R.id.album_details_txtTitle);
		expandView = (ExpandableListView) view.findViewById(R.id.expandableListView1);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		expandView.setEmptyView(mView_EmptyLoading);
		progressBar = (ProgressBar) mView_EmptyLoading.findViewById(R.id.empty_loading_progress);
		imgEmpty = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_NoItem);
		imgError = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_Error);
		txtStatus = (TextView) mView_EmptyLoading.findViewById(R.id.empty_loading_text);
		shareAlbum=(RelativeLayout) view.findViewById(R.id.layout_album_detail_share);
		shareAlbum.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String name=albumObject.getTXNAME();
				String link=albumObject.getTXURL();
				TagName.showLogError("Share", "name: "+name);
				TagName.showLogError("Share", "link: "+link);
				BaseEngine.shareTextUrl(context, name, link);				
			}
		});
	}

	void showAlbumDetail(AlbumObject albumObject) {
//		ImageLoader.getInstance().displayImage(albumObject.getAVATAR(), albumPic, options);
		if (albumObject.getAVATAR()!=null) {
			imageLoader.get(albumObject.getAVATAR(), ImageLoader.getImageListener(albumPic,
					R.drawable.ic_default, R.drawable.ic_default));
		}
		
		TagName.showLogError("Share", "link: "+albumObject.getTXURL());
		txtTitle.setText(albumObject.getTXNAME());
		txtArtist.setText(albumObject.getNAME_SINGERS());
		if (listSongs != null && expandView != null)
			if (adapter==null) {
//				TagName.showLogError("Album", "adapter null!!!!!!!!!!!!!!");
				adapter=new ExpandableSongAdapter2(context, expandView, listSongs);
				expandView.setAdapter(adapter);
			}
			else{
//				TagName.showLogError("Album", "adapter update_+______________");
				adapter.update(listSongs);
			}
	}
}
